package com.magenta.maxoptra.gis.datasources;

import com.magenta.maxoptra.gis.datasources.jaxb.ColumnType;
import com.magenta.maxoptra.gis.datasources.jaxb.TableType;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Sergey Grachev
 */
public final class GisTable extends BaseClass {
    private final static GisTableColumn NULL_COLUMN = new GisTableColumn();

    private final String alias;
    private final Map<String, GisTableColumn> columns = new HashMap<String, GisTableColumn>(0);

    GisTable() {
        this.isNull = true;
        this.alias = this.name = null;
    }

    GisTable(final TableType tableType) {
        this.alias = tableType.getAlias();
        this.name = tableType.getName();
        for (final ColumnType columnType : tableType.getColumns().getColumn()) {
            columns.put(columnType.getAlias(), new GisTableColumn(columnType));
        }
    }

    /**
     * Internal name of table
     */
    public String alias() {
        return alias;
    }

    /**
     * External name of table
     */
    public String name() {
        return name;
    }

    /**
     * Get column by internal name
     */
    public GisTableColumn column(final String alias) {
        return columns.containsKey(alias) ? columns.get(alias) : NULL_COLUMN;
    }

    @Override
    public String toString() {
        return "GisTable{" +
                "alias='" + alias + '\'' +
                ", name='" + name + '\'' +
                ", columns=" + columns +
                '}';
    }
}
